package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import net.example.utils.SerializeHelper;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class GroupBan4UserContract {

    public GroupBan4UserContract() { }

    /**
     * 群禁言协议体
     * @param groupID 群ID
     * @param operatorID 操作人ID
     * @param userID 被禁言人ID
     * @param comment 备注
     * @param minutes 禁言时长（单位：分钟
    * */
    public GroupBan4UserContract(String groupID, String operatorID, String userID, String comment, double minutes)
    {
        this.GroupID = groupID;
        this.OperatorID = operatorID;
        this.UserID = userID;
        this.Comment = comment;
        this.Minutes = minutes;
    }

    public String GroupID ;
    public String OperatorID ;
    public String UserID ;
    public String Comment ;
    /// <summary>
    /// 禁言时长（单位：分钟）
    /// </summary>
    public double Minutes ;

    public byte[] serialize() throws Exception {
        byte[] bComment = Comment.getBytes(StandardCharsets.UTF_8);
        byte[] bGroupID = GroupID.getBytes(StandardCharsets.UTF_8);
        byte[] bOperatorID = OperatorID.getBytes(StandardCharsets.UTF_8);
        byte[] bUserID = UserID.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 +bComment.length + 4 +bGroupID.length +8  + 4+bOperatorID.length+ 4 + bUserID.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bComment.length);
        body.writeBytes(bComment);
        body.writeInt(bGroupID.length);
        body.writeBytes(bGroupID);
        body.writeDouble(Minutes);
        body.writeInt(bOperatorID.length);
        body.writeBytes(bOperatorID);
        body.writeInt(bUserID.length);
        body.writeBytes(bUserID);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(), 0, result, 0, result.length);
        return result;
    }

    public void deserialize(byte[] info) throws IOException {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.Comment = SerializeUtils.readStrIntLen(buffer);
        this.GroupID = SerializeUtils.readStrIntLen(buffer);
        this.Minutes = buffer.readDouble();
        this.OperatorID = SerializeUtils.readStrIntLen(buffer);
        this.UserID = SerializeUtils.readStrIntLen(buffer);
    }
}
